package com.xpec.c4.game.datamanager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.service.module.BasicServiceModule;

/**
 * 
 * @author yangbingyan
 *
 */
public class DataProcessClassManager {
	
	/**
	 * Log管理器
	 */
	private static final Log log = LogFactory.getLog(DataProcessClassManager.class);	

	/**
	 * 
	 */
	private BasicServiceModule curModule;
	
	/**
	 * 
	 * @param module
	 */
	public DataProcessClassManager(BasicServiceModule module){
		curModule = module;
	}
	
	/**
	 * 接收資料處理結果<br>
	 * 並將返回的數據分派到對應的處理方法
	 * 
	 * @param obj
	 */
	public void dataProcessResponse(BaseDataInfo obj) {
		// 返回资料给注册的class
		// 这个不需要了, 改用callback的方式
		// dataProcessReceiveRequest(obj, null);

		// 调用callback, 如果有的话
		long sn = obj.getSnCallback();
		if(log.isInfoEnabled()){
			log.info(GameUtility.getCurMethodName() + " Callback SN = " + sn);
		}
		
		CallbackMethodInfo info = curModule.getCallbackMethodInfoBySn(sn);
		if (info == null) {
			if(log.isErrorEnabled()){
				log.error(GameUtility.getCurMethodName() + " info = null!!!!");
			}
			return;
		}

		DataProcessCallback callback = info.getCallback();

		if (callback != null) {
			try{
				// 調用預處理callback
				callback.dataProcessCallback(info.getClassOfPreProcMethod(), info.getPreProcMethod(), obj,
						info.getUserData());

				// 調用callback
				callback.dataProcessCallback(info.getClassOfMethod(), info.getMethod(), obj, info.getUserData());

			} catch (Exception e) {
				if(log.isErrorEnabled()){
					log.error(GameUtility.getCurMethodName(),e);
				}
			}

			curModule.removeDataProcessCallbackAndUserDataBySn(sn);

			// 檢查 callback有沒有被移除掉
			info = curModule.getCallbackMethodInfoBySn(sn);

			if (info != null) {
				log.warn("Callback remove faild! sn id: " + sn);
			}
		}else{
			if(log.isErrorEnabled()){
				log.error(GameUtility.getCurMethodName() + " callback = null!!!!");
			}
		}
	}	
	

}
